import * as Cesium from 'cesium'
interface CameraPos {
  longitude: number
  latitude: number
  height: number
  heading: number
  pitch: number
}
/**
 * 获取相机当前位置的经纬度
 * @param viewer
 * @returns
 */
export function getCameraLocation(viewer: Cesium.Viewer): CameraPos {
  const position = viewer.camera.position
  const direction = viewer.camera.direction
  const cartographic = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position)
  // 经度
  const longitude = Cesium.Math.toDegrees(cartographic.longitude)
  // 纬度
  const latitude = Cesium.Math.toDegrees(cartographic.latitude)
  // 高度
  const height = cartographic.height
  // 计算航向
  let heading = Cesium.Math.toDegrees(Math.atan2(direction.x, direction.y))
  if (heading < 0) {
    heading += 360
  }
  // 计算俯仰角
  const pitch = Cesium.Math.toDegrees(Math.asin(-direction.z))
  return {
    longitude,
    latitude,
    height,
    heading,
    pitch,
  }
}
/**
 * 添加监听器，输出当前视图的相机信息
 * @param viewer
 */
export function listenCameraLocaion(viewer: Cesium.Viewer) {
  //监听相机移动事件
  viewer.camera.moveStart.addEventListener(function () {
    // console.log(`监测到相机移动`)
  })
  viewer.camera.moveEnd.addEventListener(function () {
    const location = getCameraLocation(viewer)
    console.log(`-------相机当前位置----
    经度:${location.longitude}
    纬度:${location.latitude}
    高度:${location.height}
    航向:${location.heading}
    俯仰角:${location.pitch}`)
  })
}
